﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Media;

namespace DoAnHuongRung
{
    public partial class frmKhachHang : Form
    {
        public frmKhachHang()
        {
            InitializeComponent();
        }

        
        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                KhachHangDTO kh = new KhachHangDTO();
                kh.TenKH = txtTenKH.Text;
                if (rdbNamKH.Checked == true)
                {
                    kh.GioiTinh = "Nam";
                }
                else
                {
                    kh.GioiTinh = "Nữ";
                }
                kh.NgaySinh = dtpNgaySinhKH.Value.ToString("MM/dd/yyyy");
                kh.DiaChi = txtDiaChiKH.Text;
                kh.SoDienThoai = txtSDTKH.Text;
                bool b = KhachHangBUS.Insert(kh);
                if (b == true)
                {
                    SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Notify);
                    s.Play();
                    LoadKhachHang();
                    MessageBox.Show("Đã thêm khách hàng !!!");
                }
            }
            catch (Exception ex)
            {
                SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Error);
                s.Play();
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKhachHang()
        {
            dtgKhachHang.DataSource = KhachHangBUS.SelectAll();
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {

        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keydata)
        {
            if (keydata == Keys.Enter)
            {
                if (txtTimKiem.Focused == true && !txtTimKiem.Text.Equals(""))
                {
                    dtgKhachHang.AutoGenerateColumns = false;
                    dtgKhachHang.DataSource = KhachHangBUS.Select_by_Name(txtTimKiem.Text);
                }
            }
            return false;
        }

        private void txtTimKiem_Click(object sender, EventArgs e)
        {
            if (txtTimKiem.Text.Equals("Tìm kiếm"))
            {
                txtTimKiem.Text = "";
                txtTimKiem.ForeColor = Color.Black;
            }
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            LoadKhachHang();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                bool b = KhachHangBUS.Delete(int.Parse(dtgKhachHang.CurrentRow.Cells[0].Value.ToString()));
                if (b == true)
                {
                    SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Notify);
                    s.Play();
                    LoadKhachHang();
                    MessageBox.Show("Đã xóa khách hàng !!!");
                }
            }
            catch (Exception ex)
            {
                SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Error);
                s.Play();
                MessageBox.Show(ex.Message);
            }
        }

        private void dtgKhachHang_SelectionChanged(object sender, EventArgs e)
        {
            txtTenKH.Text = dtgKhachHang.CurrentRow.Cells[1].Value.ToString();
            if (dtgKhachHang.CurrentRow.Cells[2].Value.ToString().Equals("Nam"))
            {
                rdbNamKH.Checked = true;
            }
            else
            {
                rdbNuKH.Checked = true;
            }
            dtpNgaySinhKH.Text = dtgKhachHang.CurrentRow.Cells[3].Value.ToString();
            txtDiaChiKH.Text = dtgKhachHang.CurrentRow.Cells[4].Value.ToString();
            txtSDTKH.Text = dtgKhachHang.CurrentRow.Cells[5].Value.ToString();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                KhachHangDTO kh = new KhachHangDTO();
                kh.MaKH = int.Parse(dtgKhachHang.CurrentRow.Cells[0].Value.ToString());
                kh.TenKH = txtTenKH.Text;
                if (rdbNamKH.Checked == true)
                {
                    kh.GioiTinh = "Nam";
                }
                else
                {
                    kh.GioiTinh = "Nữ";
                }
                kh.NgaySinh = dtpNgaySinhKH.Value.ToString("MM/dd/yyyy");
                kh.DiaChi = txtDiaChiKH.Text;
                kh.SoDienThoai = txtSDTKH.Text;
                bool b = KhachHangBUS.Update(kh);
                if (b == true)
                {
                    SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Notify);
                    s.Play();
                    LoadKhachHang();
                    MessageBox.Show("Đã cập nhật khách hàng !!!");
                }
            }
            catch (Exception ex)
            {
                SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Error);
                s.Play();
                MessageBox.Show(ex.Message);
            }
        }

        private void dtgKhachHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            LoadKhachHang();
        }
    }
}
